package com.china08.yxyapi.api.manage.tec.teaching.zujuan.manualpaper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.manage.tec.teaching.zujuan.ZujuanQuestionService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.ApiOperation;

/**
 * 手动组卷-我的题库-根据组查询试题
 * 
 */
@CrossOrigin
@RestController
public class ZujuanQuesListByMyGroupController extends YxyApiBaseController {

    @Autowired
    private ZujuanQuestionService zujuanQuestionService;

    @ApiOperation(value = "根据组查询试题")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/myques/mygroup/queslist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        YxyProfile profile = getProfile(request, response);
        String teacherId = profile.getId();
        String schoolId = profile.getSchoolId();
        List<RespModel> respModels = new ArrayList<RespModel>();
        Pageable pageable = reqModel.getPageable(Direction.DESC, "created_date");
        Page<ElasticYxyYxyQuesResource> quesPage = zujuanQuestionService.questionFindByMyGroupId(schoolId, teacherId,
                reqModel.groupId, reqModel.level, reqModel.type, reqModel.phase, reqModel.subject, pageable);
        for (ElasticYxyYxyQuesResource elasticYxyYxyQuesResource : quesPage) {
            RespModel model = new RespModel();
            model.knowIds = elasticYxyYxyQuesResource.getKnowledgeIds();
            model.difficultyLevel = elasticYxyYxyQuesResource.getDifficultyLevel();
            model.id = elasticYxyYxyQuesResource.getId();
            model.knowledgeTexts = elasticYxyYxyQuesResource.getKnowledgeTexts();
            model.quesBody = elasticYxyYxyQuesResource.getQuesBody();
            model.questionType = elasticYxyYxyQuesResource.getQuestionType();
            model.isMy = StringUtils.equals(elasticYxyYxyQuesResource.getCreatedBy(), teacherId);
            // model.quesTimes =
            // zujuanQuestionService.countQuestionUse(model.id);
            model.quesTimes = (elasticYxyYxyQuesResource.getHomeworkUseCount() == null ? 0
                    : elasticYxyYxyQuesResource.getHomeworkUseCount());
            respModels.add(model);
        }
        Page<RespModel> respPage = new PageImpl<ZujuanQuesListByMyGroupController.RespModel>(respModels, pageable,
                quesPage.getTotalElements());
        return respPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 学段id
        public String phase;
        // 学科id
        public String subject;
        // 组id
        public String groupId;
        // 题的难度级别
        public String level;
        // 题型
        public String type;
    }

    protected static class RespModel {
        // 试题id
        public String id;
        // 题的难度级别
        public String difficultyLevel;
        // 题的知识点名称
        public List<String> knowledgeTexts;

        // 知识点集合
        public List<Integer> knowIds;
        // 题型
        public String questionType;
        // 题干
        public Object quesBody;
        // 题的使用次数
        public Long quesTimes;
        // 是否我的试题
        public boolean isMy;
    }
}
